<?php 

Class adminModel extends appModel
{
	/**
	 * 获取权限数据
	 * @return 
	 */
	function get_admin_action()
	{
		$sql = "SELECT action_id, action_list FROM ".$this->table('admin_action_list');
		$action = $this->getAll($sql);
		foreach($action as $k => $v)
		{
			$sql = "select mod_id, mod_name, mod_code, action_id as parent_id from ".$this->table('admin_action')." where action_id=".$v['action_id'];
			$child = $this->getAll($sql);
			$action[$k]['child'] = $child;
		}
		return $action;
	}
	
	/**
	 * 用户角色是否已存在
	 * @param string $rolename 用户名
	 * @return boolean
	 */
	public function roleEisted($rolename, $id = '')
	{
		$where = "";
		if(!empty($id))
		{
			$where = " and id!=".$id;
		}
		
		$sql="SELECT count(*) FROM ".$this->table('admin_role')." WHERE role_name = '".$rolename."'".$where;
			
		$rolenum=$this->getOne($sql);
		if($rolenum>0)
		{
			return false;
		}
		else 
		{
			return true;
		}
	}
	
	/**
	 * 获取用户组
	 * @return array
	 */
	public function roleList($page, $limit = 10)
	{
		$sql = "select * FROM ".$this->table('admin_role');
		$data = $this->show_page($sql, $page, $limit);
		return $data;
	}
	
	/**
	 * 写入角色信息
	 * @param $info
	 * @return boolean
	 */
	public function insertRole($info)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "insert into ".$this->table('admin_role')." (".$data['key'].") values (".$data['val'].")";
			$this->query($sql);
			return $this->insert_id();
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 删除角色
	 * @param $id
	 * @return 
	 */
	public function del_role($id)
	{
		$sql="DELETE FROM ".$this->table('admin_role')." WHERE id='".$id."'";
		$res=$this->query($sql);
		return true;
	}
	
	/**
	 * 删除管理员信息
	 * @param $id
	 * @return 
	 */
	public function del_user($id)
	{
		if(!empty($id) && $id != 0)
		{
			$sql="DELETE FROM ".$this->table('admin')." WHERE id='".$id."'";
			$res=$this->query($sql);
			return true;
		}
		else 
		{
			return false;
		}		
	}
	
	/**
	 * 更新角色信息
	 * @param $info
	 * @param $id
	 * @return 
	 */
	public function updateRole($info, $id)
	{
		if($info)
		{
			$data = yf_set_update($info);
			$sql = "update ".$this->table('admin_role')." set ".$data." where id=".$id;
			$this->query($sql);
			return true;
		}
		else
		{
			return false;
		}
	}
	function get_role_list()
	{
		$sql="SELECT id,role_name FROM ".$this->table('admin_role');
		$role=$this->getAll($sql);
		return $role;
	}
	
	public function getRoleInfo($id)
	{
		$sql="SELECT * FROM ".$this->table('admin_role')." WHERE id = '".$id."'";
		$role=$this->getRow($sql);
		return $role;
	}
	
	/**
	 * 获得管理员信息
	 * @param string $id
	 * @return
	 */
	public function getAdminInfo($id = '')
	{	
		if(session('admin_id'))
		{
			if($id)
			{
				$sql="SELECT qq,email,username,group_id FROM ".$this->table('admin')." WHERE id = '".$id."'";
				$result=$this->getRow($sql);
				if($result)
				{
					return $result;
				}
			}
			else 
			{
				$sql="SELECT qq,email,username FROM ".$this->table('admin')." WHERE id = '".session('admin_id')."'";
				$result=$this->getRow($sql);
				if($result)
				{
					return $result;
				}
				else 
				{
					return $result=array();
				}
			}
		}
		else 
		{
			return $result=array();
		}
	}
	
	/**
	 * 插入用户资料时验证信息是否已存在
	 * @param string $username 用户名
	 * @param string $email 电子邮箱
	 * @return string
	 */
	public function isExist($arr)
	{
		foreach($arr as $k => $v)
		{
			if($k == 'email' || $k == 'username')
			{
				$sql="SELECT count(*) FROM ".$this->table('admin')." WHERE $k='$v'";
				$query=$this->getOne($sql);
				if($query>0)
				{
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 验证更新用户资料时的准确性
	 * @param string $username 用户名
	 * @param string $email 电子邮箱
	 * @return boolean
	 */
	function checkInfo($username,$email,$id = '')
	{   
		if(empty($id))
		{
			$id = session('admin_id');
		}
		
// 		$sql="SELECT email FROM ".$this->table('admin')." WHERE id='".$id."'";
// 		$oldemail = $this->getOne($sql);
// 		$email_exist = 0;
// 		if($oldemail != $email)
// 		{
// 			$sql="SELECT count(*) FROM ".$this->table('admin')." WHERE email='$email'";
// 			$email_exist=$this->getOne($sql);
// 		}
		
// 		if($email_exist>0)
// 		{
// 			return false;
// 		}
		
		//验证用户名是否相同
		$sql = "SELECT username FROM ".$this->table('admin')." WHERE id='".$id."'";
		$oldname = $this->getOne($sql);
		$name_exist = 0;
		
		if($oldname != $username)
		{
			$sql="SELECT count(*) FROM ".$this->table('admin')." WHERE username='$username'";
			$name_exist=$this->getOne($sql);
		}
		
		if($name_exist>0)
		{
			return false;
		}
		return true;
	}
   
	/**
	 * 获取所有用户
	 * @param 
	 * @return array
	 */
	function adminList($page, $limit = 10)
	{
		$listPage=array('data'=>'','pageLink'=>'');
	
		$result=$this->show_page("SELECT a.id, a.username, a.created, a.status, (SELECT b.role_name from ".$this->table('admin_role')." as b WHERE a.group_id=b.id) as group_name FROM ".$this->table('admin')." as a", $page, $limit);
		$listPage['pagelink']=$result['page'];
	    $listPage['data']=$result['result'];
		return $listPage;
	}
	function search_admin($page=1,$keyword)
	{
		$listPage=array('data'=>'','pageLink'=>'');
	
		if($keyword)
		{
			$sql="SELECT a.id,a.username,(SELECT b.role_name from ".$this->table('admin_role')." as b WHERE a.group_id=b.id) as group_name FROM ".$this->table('admin')." as a WHERE username like '%".$keyword."%'";
	
			$result=$this->show_page($sql,$page,'10');
			$listPage['pagelink']=$result['page'];
			$listPage['data']=$result['result'];
	
		}
	
		return $listPage;
	}
	function search_user($page=1,$keyword)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		
		if($keyword)
		{
		$sql="SELECT id,integral,phone FROM ".$this->table('users')."  WHERE phone like '%".$keyword."%'";
		
		$result=$this->show_page($sql,$page,'10');
		$listPage['pagelink']=$result['page'];
	    $listPage['data']=$result['result'];
		
		}
	
		return $listPage;
	}
	
	
	/**
	 * 添加新用户
	 * @param array $info
	 * @return boolean
	 */
	public function insertInfo($info)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "insert into ".$this->table('admin')." (".$data['key'].") values (".$data['val'].")";
			$this->query($sql);
			if($this->insert_id())
			{
				return true;
			}
			else 
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 超级管理员修改用户资料
	 * @param array $info
	 * *@param int $id
	 * @return boolean
	 */
	public function updateInfo($info, $id)
	{
		$where = '';
		$update_pwd = false;
		$res = null;
		if($id)
		{
			$sql = "SELECT group_id FROM ".$this->table('admin')." WHERE id = '".session('admin_id')."'";
			$group_id = $this->getOne($sql);
			if($group_id == 1)//超级管理员才有权限
			{
				if(!empty($info['password']))
				{
					$update_pwd = true;
				}			
				$data = yf_set_update($info);		
				$sql = "UPDATE ".$this->table('admin')." SET ".$data." WHERE id='".$id."'";
				if($res = $this->query($sql))
				{
					if($id == session('admin_id')&&$update_pwd)
					{
						cookie('QNHCMS[admin_id]',   '');
						cookie('QNHCMS[admin_pass]', '');
						session('[destroy]');
					}
				}
			}
		}
		return $res;
	}
	
	/**
	 * 保存用户资料
	 * @param array $info
	 * @return boolean
	 */
	function saveInfo($info=array())
	{
		$where='';
		$update_pwd=false;
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				if($k=='password')
				{
					$where.=" $k='".md5($v)."',";
					$update_pwd=true;
				}
				else 
				{
					$where.=" $k='$v',";
				}
			}
		}
		$where.="modified=".$_SERVER['REQUEST_TIME'];
	
		$result==array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('admin')." SET ".$where." WHERE id='".session('admin_id')."'";
		if($res=$this->query($sql))
		{
			if($update_pwd)
			{
				$result['error']=-1;
			}
			
		}
		else 
		{
			$result['error']=1;
			$result['content']=Lang('SQL_ERROR');
		}
		return $result;
		//invalid_operation
	}
	
	/**
	 * 添加权限栏目数据
	 * @param $info;插入的数据，格式为关联数组
	 * @return boolean
	 */
	public function insert_action($info)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "insert into ".$this->table('admin_action_list')." (".$data['key'].") values (".$data['val'].")";
			$this->query($sql);
			if($this->insert_id())
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 获得权限栏目数据
	 * @param $page;页码
	 * @param $limit;没有显示条数
	 * @return $data;$data['result']:数据$data['page']分页代码
	 */
	public function get_action_list($page, $limit = 10)
	{
		$sql = "select * FROM ".$this->table('admin_action_list');
		$data = $this->show_page($sql, $page, $limit);
		return $data;
	}
	
	/**
	 * 获得权限栏目的名称
	 * @param $id;权限栏目的ID
	 * @return action_list;
	 */
	public function get_action_info($id)
	{
		$sql = "select action_list from ".$this->table('admin_action_list')." where action_id=".$id;
		return $this->getOne($sql);
	}
	
	/**
	 * 修改权限栏目名称
	 * @param $data;栏目名称
	 * @param $id;栏目ID
	 * @return boolean;
	 */
	public function update_action($data, $id)
	{
		$sql = "update ".$this->table('admin_action_list')." set `action_list`='".$data."' where action_id=".$id;
		$this->query($sql);
		return true;
	}
	
	/**
	 * 插入权限详细信息
	 * @param $info;插入的关联数组
	 * @return boolean;
	 */
	public function ins_act($info)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "insert into ".$this->table('admin_action')." (".$data['key'].") values (".$data['val'].")";
			$this->query($sql);
			if($this->insert_id())
			{
				$this->query("update ".$this->table('admin_action_list')." set action_child=action_child+1 where action_id=".$info['action_id']);
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 获得详细权限列表
	 * @param $id;权限栏目ID
	 * @param $page;页码
	 * @param $limit;每页几条数据
	 * @return $list;数组
	 */
	public function get_mod_list($id, $page, $limit = 10)
	{
		$sql = "select * FROM ".$this->table('admin_action')." where action_id=".$id;
		$list = $this->show_page($sql, $page, $limit);
		return $list;
	}
	
	/**
	 * 获得详细权限数据
	 * @param $id;详细权限ID
	 * @return 数组
	 */
	public function get_one_mod($id)
	{
		$sql = "select * from ".$this->table('admin_action')." where mod_id=".$id;
		return $this->getRow($sql);
	}
	
	/**
	 * 修改权限详细信息
	 * @param $info;要修改的信息
	 * @param $id;对应的ID
	 * @return boolean;
	 */
	public function ud_act($info, $id)
	{
		$data = yf_set_update($info);
		$sql = "update ".$this->table('admin_action')." set ".$data." where mod_id=".$id;
		$this->query($sql);
		return true;
	}
}